package Search;

import Data.FriendListData;
import Data.PrivateInformationData;
import Data.event;
import MyInterfaces.MassageType;
import Server.ManageClientConnectionServerThread;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.*;
import java.util.ArrayList;
import java.util.Map;

public class SetFriendList {

    private String userid;
    String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = "root";
    String password = "Mi20030610.";

    private FriendListData friendListData;

    public SetFriendList(String userid,FriendListData friendListData) {
        this.userid=userid;
        this.friendListData = friendListData;
    }

    public ArrayList<PrivateInformationData> getlist() throws IOException, SQLException, ClassNotFoundException {
        String onesql="SELECT * FROM userinformation where id in (select userFId FROM friendlist where userId=?);";
        String twosql="SELECT * FROM userinformation where id in (select userId FROM friendlist where userFId=?);";

        System.out.println("读取数据库好友");
        Connection Conn = DriverManager.getConnection(url, user, password);
        PreparedStatement operation = null;
        operation = Conn.prepareStatement(onesql);
        operation.setString(1,userid);
        ResultSet resultSet=operation.executeQuery();
        System.out.println(userid);
//        ResultSetMetaData md=resultSet.getMetaData();//获取键名称
//        int col=md.getColumnCount();//行的数量
        ArrayList<PrivateInformationData> arrayList=new ArrayList<>();
        while(resultSet.next())
        {
            System.out.println("找到了"+userid);
            PrivateInformationData e=new PrivateInformationData();

            String path=resultSet.getString("avatar");
            File file=new File(path);
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] t=new byte[(int) file.length()];
            fileInputStream.read(t);
            e.setAvatar(t);
            fileInputStream.close();

            e.setUserNickName(resultSet.getString("userNickName"));
            e.setSex(resultSet.getString("sex"));
            e.setBirth(resultSet.getDate("birth"));
            e.setSaying(resultSet.getString("saying"));
            e.setEmailNumber(resultSet.getString("emailNumber"));
            e.setUserid(resultSet.getString("id"));
            System.out.println(e.getAvatar()+" "+e.getSex()+" "
            +e.getEmailNumber()+" "+e.getUserid()+" "+e.getUserNickName()
                    +" "+e.getBirth());

            arrayList.add(e);
        }
        //添加俩次
        operation=Conn.prepareStatement(twosql);
        operation.setString(1,userid);
        resultSet=operation.executeQuery();
//        md=resultSet.getMetaData();//获取键名称
//        col=md.getColumnCount();//行的数量
        while(resultSet.next())
        {
            PrivateInformationData e=new PrivateInformationData();

            String path=resultSet.getString("avatar");
            File file=new File(path);
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] t=new byte[(int) file.length()];
            fileInputStream.read(t);
            e.setAvatar(t);
            fileInputStream.close();

            e.setUserNickName(resultSet.getString("userNickName"));
            e.setSex(resultSet.getString("sex"));
            e.setBirth(resultSet.getDate("birth"));
            e.setSaying(resultSet.getString("saying"));
            e.setEmailNumber(resultSet.getString("emailNumber"));
            e.setUserid(resultSet.getString("id"));
            System.out.println(e.getAvatar()+" "+e.getSex()+" "
                    +e.getEmailNumber()+" "+e.getUserid()+" "+e.getUserNickName()
                    +" "+e.getBirth());


            arrayList.add(e);
        }

        String newsql="select *FROM friendlist where ((userId=? and userFId=?) or (userId=? and userFId=?));";
        operation=Conn.prepareStatement(newsql);
        for (int i = 0; i < arrayList.size(); i++) {

            operation.setString(1,userid);
            operation.setString(2,arrayList.get(i).getUserid());
            operation.setString(3,arrayList.get(i).getUserid());
            operation.setString(4,userid);

            resultSet=operation.executeQuery();
            if(resultSet.next())
            {
                arrayList.get(i).setIsfriend(resultSet.getInt("isfriend"));
                //需要判断是不是自己发出去的
                if(arrayList.get(i).getIsfriend()==2)
                {
                    String isid=resultSet.getString("userId");
                    System.out.println(userid+" "+isid);
                    if(userid.equals(isid))
                    {
                        //    System.out.println(userid+" "+isid);
                        //如果是移除掉该元素
                        arrayList.remove(i);

                    }
                    else
                    {
                        //如果

                    }
                }
                else
                {
                    if(ManageClientConnectionServerThread.existId(arrayList.get(i).getUserid()))
                    {
                        arrayList.get(i).setIsonline(1);
                        System.out.println(arrayList.get(i).getUserid()+"在线，给它发送消息");
                        Socket freshFriend=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getUserid());
                        PrivateInformationData fdata=new SearchInformationById(userid).getPIDdata();
                        fdata.setIsonline(1);
                        fdata.setIsfriend(1);
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Fresh_Friend_Status);
                        returnmassage.setT(fdata);
                        ObjectOutputStream writer=new ObjectOutputStream(freshFriend.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }
                }
            }
        }

        operation.close();
        resultSet.close();
        Conn.close();
        return arrayList;
    }
}

